package com.hzj.recipe.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzj.recipe.entity.Category;
import com.hzj.recipe.entity.Recipe;
import com.hzj.recipe.mapper.RecipeMapper;
import com.hzj.recipe.service.CategoryService;
import com.hzj.recipe.mapper.CategoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;
import java.util.Objects;

/**
* @author 蜗牛菌
* @description 针对表【category(食谱分类表)】的数据库操作Service实现
* @createDate 2024-01-04 10:49:47
*/
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category>
    implements CategoryService{

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private RecipeMapper recipeMapper;

    //分页查询所有数据
    public Object listAll(Integer index, Integer size, Category category){
        //默认第一页开始，每页5条
        if(index == null ||index==0){
            index=1;
        }
        if(size==null||size==0){
            size=5;
        }
        Page<Category> pageInfo = new Page(index, size);
        LambdaQueryWrapper<Category> lambdaQueryWrapper = new LambdaQueryWrapper();
        if (category.getCategoryId() != null && category.getCategoryId() != 0 && !Objects.equals(category.getCategoryId(), "")) {
            lambdaQueryWrapper.eq(Category::getCategoryId, category.getCategoryId());
        }
        if ((category.getCategoryName()!=null) && !Objects.equals(category.getCategoryName(), "")) {
            lambdaQueryWrapper.like(Category::getCategoryName, category.getCategoryName());
        }
        if ((category.getCategoryDescription()!=null) && !Objects.equals(category.getCategoryDescription(), "")) {
            lambdaQueryWrapper.like(Category::getCategoryDescription, category.getCategoryDescription());
        }
        Page<Category> categoryPage = categoryMapper.selectPage(pageInfo, lambdaQueryWrapper);

        return categoryPage;
    }

    //通过主键分页查询一个分类下的所有食谱名单
    @Override
    public Object listRecipeById(Integer index, Integer size, Serializable id) {
        //默认第一页开始，每页5条
        if(index == null ||index==0){
            index=1;
        }
        if(size==null||size==0){
            size=5;
        }
        Page<Recipe> pageInfo = new Page(index, size);

        LambdaQueryWrapper<Recipe> lambdaQueryWrapper = new LambdaQueryWrapper();
        if (categoryMapper.selectById(id)!=null) {
            lambdaQueryWrapper.eq(Recipe::getCategoryId, id);
            return recipeMapper.selectPage(pageInfo, lambdaQueryWrapper);
        }
        return null;
    }

    ;
}




